home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 9647 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.7 KB  |  102 lines

  1. Path: andrew.cmu.edu!sangp+
  2. From: "Sang Hyun Park, Shawn" <sangp+@andrew.cmu.edu>
  3. Newsgroups: comp.lang.c++
  4. Subject: Reading binary file in MSVC++2.1 ??
  5. Date: Sun,  3 Mar 1996 05:16:41 -0500
  6. Organization: Junior, Social & Decision Sciences, Carnegie Mellon, Pittsburgh, PA
  7. Message-ID: <0lCL69200YUf0EAnE0@andrew.cmu.edu>
  8. NNTP-Posting-Host: po6.andrew.cmu.edu
  9.  
  10.  
  11. I'm trying to read in a binary file into an array of fixed, but large size.
  12. (the size of files i'm reading in is approx. 12k)
  13. I tried two different ways to read in the file, but i kept getting error
  14. messages.  could someone take a look it and suggest me what i'm doing wrong 
  15. or missing in the code?  Or please tell me if there is a better way to
  16. accomplish the task.
  17.  
  18. another question i have is, how can i display the content of the binary
  19. file on screen?  there is some header information in the input file that
  20. i need to skip, and i want to see how many bytes i need to skip from the
  21. beginning of the file.
  22.  
  23. i'm doing this under WinNT with MSVC++ 2.1
  24.  
  25. thank you,
  26. shawn park
  27.  
  28.  
  29. trial-1
  30. ------------------------------------------------------------------------
  31. #include <fstream.h>
  32. #include <string.h>
  33.  
  34. void main(void)
  35. {
  36.  short buf[2000];    // arbitrary size for trial
  37.   
  38.  ifstream ins("filename.bin");
  39.  ins.get(buf, 2000);   *
  40.  
  41. }
  42. --------------------------------------
  43. "filename.bin" is the name of the binary file.
  44.  
  45. Error message i got was:
  46. * `class istream &istream::get(char*,int,char)': 
  47.        cannot convert parameter 1 from 'short[2000]' to 'char*'
  48.  
  49. so, i think it might be that i can only use ins.get to read in chars, and
  50. no binary chars.   is that correct?
  51.  
  52.  
  53.  
  54. trial-2
  55. -------------------------------------------------------------
  56. #include <afx.h>
  57. #include <iostream.h>
  58.  
  59. const size_t BUFSIZE = 256;
  60.  
  61. // Open a CFile associated with a specific file
  62.         CFile fin("Filename.bin", CFile::modeRead);
  63.  
  64. // Open test output file
  65.         CFile fout("output.xxx", CFile::modeWrite |
  66.                                CFile::modeCreate);
  67.  
  68. // Read from one file and write to the other
  69.         short buf[BUFSIZE];
  70.         int n = fin.Read(buf, BUFSIZE);
  71.  
  72.         while (n > 0)
  73.         {
  74.             fout.Write(buf, n);
  75.             n = fin.Read(buf, BUFSIZE);
  76.         };
  77.  
  78.         fin.Close();
  79.         fout.Close();
  80.  
  81. }
  82. ------------------------------------------
  83.  
  84. according to the book i have, i can read in binary information using the
  85. CFile class (as above), but i kept getting this error message i couldn't
  86. understand.
  87.  
  88.  
  89. 1) nafxcwd.lib(thrdcore.obj) : error LNK2001: unresolved external symbol
  90. "__beginthreadex"
  91. 2) nafxcwd.lib(thrdcore.obj) : error LNK2001: unresolved external symbol
  92. "__endthreadex"
  93.  
  94.  
  95. i thought i wasn't dealing with thread. what is wrong with this method of
  96. reading in the file?
  97.  
  98.  
  99.  
  100.  
  101.  
  102.